import extend from 'extend'
import { modal } from 'vue-strap'
import { updateMiniAd, showMsg } from '../../../vuex/action'

const validation = {
  ad(data) {
    for (let name in data) {
      if ((data[name] + '').length === 0) {
        return false
      }
    }
    return true
  }
}
export default {
  name: 'MiniAdUpdate',
  props: ['ad'],
  components: {
    'vue-modal': modal
  },
  data() {
    return {
      showUpdateAdModal: false
    }
  },
  vuex: {
    actions: {
      xUpdateMiniAd: updateMiniAd,
      xShowMsg: showMsg
    }
  },
  methods: {
    updateAd() {
      this.showUpdateAdModal = true
    },
    submit() {
      var ad = extend(true, {}, this.ad)
      ad.allowExtend = +ad.allowExtend

      var valid = validation.ad(ad)
      if (!valid) {
        return this.xShowMsg({
          type: 'danger',
          content: '* 标记的都是必填/必选项'
        })
      }

      this.xUpdateMiniAd(ad)
        .then(() => {
          this.showNewAdModal = false
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}